package com.zenithmind.classroom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenithmind.classroom.mapper.LearningProgressMapper;
import com.zenithmind.classroom.pojo.dto.ProgressUpdateDTO;
import com.zenithmind.classroom.pojo.domain.LearningProgress;
import com.zenithmind.classroom.pojo.vo.LearningAnalysisVO;
import com.zenithmind.classroom.pojo.vo.LearningProgressVO;
import com.zenithmind.classroom.pojo.vo.LearningStatisticsVO;
import com.zenithmind.classroom.service.LearningProgressService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 学习进度服务实现类
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LearningProgressServiceImpl extends ServiceImpl<LearningProgressMapper, LearningProgress> implements LearningProgressService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateProgress(ProgressUpdateDTO updateDTO) {
        log.info("更新学习进度: userId={}, courseId={}", updateDTO.getUserId(), updateDTO.getCourseId());
        
        return baseMapper.updateProgress(
                updateDTO.getUserId(),
                updateDTO.getCourseId(),
                updateDTO.getChapterId(),
                updateDTO.getLessonId(),
                updateDTO.getStatus(),
                updateDTO.getProgressPercentage(),
                updateDTO.getStudiedSeconds(),
                updateDTO.getLastPosition(),
                updateDTO.getDeviceType()
        ) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean startLearning(String userId, String courseId, String chapterId, String lessonId) {
        log.info("开始学习: userId={}, courseId={}", userId, courseId);
        
        return baseMapper.startLearning(userId, courseId, chapterId, lessonId, 1) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean completeLearning(String userId, String courseId, String chapterId, String lessonId) {
        log.info("完成学习: userId={}, courseId={}", userId, courseId);
        
        return baseMapper.completeLearning(userId, courseId, chapterId, lessonId) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean pauseLearning(String userId, String courseId, String chapterId, String lessonId) {
        log.info("暂停学习: userId={}, courseId={}", userId, courseId);
        
        return baseMapper.pauseLearning(userId, courseId, chapterId, lessonId) > 0;
    }

    @Override
    public LearningProgressVO getUserCourseProgress(String userId, String courseId) {
        log.info("获取用户课程进度: userId={}, courseId={}", userId, courseId);
        
        return baseMapper.selectUserCourseProgress(userId, courseId);
    }

    @Override
    public LearningProgressVO getUserChapterProgress(String userId, String chapterId) {
        log.info("获取用户章节进度: userId={}, chapterId={}", userId, chapterId);
        
        return baseMapper.selectUserChapterProgress(userId, chapterId);
    }

    @Override
    public LearningProgressVO getUserLessonProgress(String userId, String lessonId) {
        log.info("获取用户课时进度: userId={}, lessonId={}", userId, lessonId);
        
        return baseMapper.selectUserLessonProgress(userId, lessonId);
    }

    @Override
    public LearningStatisticsVO getUserLearningStatistics(String userId, LocalDateTime startTime, LocalDateTime endTime) {
        log.info("获取用户学习统计: userId={}", userId);
        
        return baseMapper.selectUserLearningStatistics(userId, startTime, endTime);
    }

    @Override
    public LearningStatisticsVO getCourseLearningStatistics(String courseId) {
        log.info("获取课程学习统计: courseId={}", courseId);
        
        return baseMapper.selectCourseLearningStatistics(courseId);
    }

    @Override
    public LearningAnalysisVO getUserLearningAnalysis(String userId, Integer days) {
        log.info("获取用户学习分析: userId={}, days={}", userId, days);
        
        return baseMapper.selectUserLearningAnalysis(userId, days);
    }

    @Override
    public List<LearningProgressVO> getLearningRanking(String courseId, Integer type, Integer limit) {
        log.info("获取学习排行榜: courseId={}, type={}", courseId, type);
        
        return baseMapper.selectLearningRanking(courseId, type, limit);
    }

    @Override
    public List<String> getLearningRecommendations(String userId, String courseId) {
        log.info("获取学习建议: userId={}, courseId={}", userId, courseId);
        
        return baseMapper.selectLearningRecommendations(userId, courseId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean recordLearningBehavior(String userId, String courseId, String lessonId, String action, Integer duration, Integer deviceType) {
        log.info("记录学习行为: userId={}, action={}", userId, action);
        
        return baseMapper.recordLearningBehavior(userId, courseId, lessonId, action, duration, deviceType) > 0;
    }

    @Override
    public List<LearningProgressVO> getLearningPath(String userId, String courseId) {
        log.info("获取学习路径: userId={}, courseId={}", userId, courseId);
        
        return baseMapper.selectLearningPath(userId, courseId);
    }

    @Override
    public LocalDateTime predictCompletionTime(String userId, String courseId) {
        log.info("预测完成时间: userId={}, courseId={}", userId, courseId);
        
        return baseMapper.predictCompletionTime(userId, courseId);
    }

    @Override
    public List<LearningAnalysisVO.HeatmapData> getLearningHeatmap(String userId, Integer year) {
        log.info("获取学习热力图: userId={}, year={}", userId, year);
        
        return baseMapper.selectLearningHeatmap(userId, year);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean syncLearningProgress(String userId, Integer deviceType) {
        log.info("同步学习进度: userId={}, deviceType={}", userId, deviceType);
        
        return baseMapper.syncLearningProgress(userId, deviceType) > 0;
    }

    @Override
    public String exportLearningReport(String userId, String courseId, String format) {
        log.info("导出学习报告: userId={}, courseId={}, format={}", userId, courseId, format);
        
        // TODO: 实现学习报告导出逻辑
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean resetLearningProgress(String userId, String courseId) {
        log.info("重置学习进度: userId={}, courseId={}", userId, courseId);
        
        return baseMapper.resetLearningProgress(userId, courseId) > 0;
    }

    @Override
    public List<String> getLearningReminders(String userId) {
        log.info("获取学习提醒: userId={}", userId);
        
        return baseMapper.selectLearningReminders(userId);
    }
}
